home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 26 / Cream of the Crop 26.iso / os2 / octa209s.zip / octave-2.09 / liboctave / Array2.cc < prev    next >
C/C++ Source or Header  |  1996-11-07  |  4KB  |  179 lines

  1. // Template array classes
  2. /*
  3.  
  4. Copyright (C) 1996 John W. Eaton
  5.  
  6. This file is part of Octave.
  7.  
  8. Octave is free software; you can redistribute it and/or modify it
  9. under the terms of the GNU General Public License as published by the
  10. Free Software Foundation; either version 2, or (at your option) any
  11. later version.
  12.  
  13. Octave is distributed in the hope that it will be useful, but WITHOUT
  14. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  15. FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  16. for more details.
  17.  
  18. You should have received a copy of the GNU General Public License
  19. along with Octave; see the file COPYING.  If not, write to the Free
  20. Software Foundation, 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  21.  
  22. */
  23.  
  24. #if defined (__GNUG__)
  25. #pragma implementation
  26. #endif
  27.  
  28. #ifdef HAVE_CONFIG_H
  29. #include <config.h>
  30. #endif
  31.  
  32. #include <cassert>
  33.  
  34. #include <iostream.h>
  35.  
  36. #include "Array2.h"
  37.  
  38. #if defined (HEAVYWEIGHT_INDEXING)
  39. #include "idx-vector.h"
  40. #include "Array2-idx.h"
  41. #endif
  42.  
  43. #include "lo-error.h"
  44.  
  45. template <class T>
  46. T
  47. Array2<T>::range_error (const char *fcn, int i, int j) const
  48. {
  49.   (*current_liboctave_error_handler)
  50.     ("%s (%d, %d): range error", fcn, i, j);
  51.   return T ();
  52. }
  53.  
  54. template <class T>
  55. T&
  56. Array2<T>::range_error (const char *fcn, int i, int j)
  57. {
  58.   (*current_liboctave_error_handler)
  59.     ("%s (%d, %d): range error", fcn, i, j);
  60.   static T foo;
  61.   return foo;
  62. }
  63.  
  64. // Two dimensional array class.
  65.  
  66. template <class T>
  67. void
  68. Array2<T>::resize (int r, int c)
  69. {
  70.   if (r < 0 || c < 0)
  71.     {
  72.       (*current_liboctave_error_handler)
  73.     ("can't resize to negative dimension");
  74.       return;
  75.     }
  76.  
  77.   if (r == dim1 () && c == dim2 ())
  78.     return;
  79.  
  80.   ArrayRep *old_rep = rep;
  81.   const T *old_data = data ();
  82.  
  83.   int old_d1 = dim1 ();
  84.   int old_d2 = dim2 ();
  85.   int old_len = length ();
  86.  
  87.   rep = new ArrayRep (r*c);
  88.  
  89.   d1 = r;
  90.   d2 = c;
  91.  
  92.   if (old_data && old_len > 0)
  93.     {
  94.       int min_r = old_d1 < r ? old_d1 : r;
  95.       int min_c = old_d2 < c ? old_d2 : c;
  96.  
  97.       for (int j = 0; j < min_c; j++)
  98.     for (int i = 0; i < min_r; i++)
  99.       xelem (i, j) = old_data[old_d1*j+i];
  100.     }
  101.  
  102.   if (--old_rep->count <= 0)
  103.     delete old_rep;
  104. }
  105.  
  106. template <class T>
  107. void
  108. Array2<T>::resize (int r, int c, const T& val)
  109. {
  110.   if (r < 0 || c < 0)
  111.     {
  112.       (*current_liboctave_error_handler)
  113.     ("can't resize to negative dimension");
  114.       return;
  115.     }
  116.  
  117.   if (r == dim1 () && c == dim2 ())
  118.     return;
  119.  
  120.   ArrayRep *old_rep = rep;
  121.   const T *old_data = data ();
  122.  
  123.   int old_d1 = dim1 ();
  124.   int old_d2 = dim2 ();
  125.   int old_len = length ();
  126.  
  127.   rep = new ArrayRep (r*c);
  128.  
  129.   d1 = r;
  130.   d2 = c;
  131.  
  132.   int min_r = old_d1 < r ? old_d1 : r;
  133.   int min_c = old_d2 < c ? old_d2 : c;
  134.  
  135.   if (old_data && old_len > 0)
  136.     {
  137.       for (int j = 0; j < min_c; j++)
  138.     for (int i = 0; i < min_r; i++)
  139.       xelem (i, j) = old_data[old_d1*j+i];
  140.     }
  141.  
  142.   for (int j = 0; j < min_c; j++)
  143.     for (int i = min_r; i < r; i++)
  144.       xelem (i, j) = val;
  145.  
  146.   for (int j = min_c; j < c; j++)
  147.     for (int i = 0; i < r; i++)
  148.       xelem (i, j) = val;
  149.  
  150.   if (--old_rep->count <= 0)
  151.     delete old_rep;
  152. }
  153.  
  154. template <class T>
  155. Array2<T>&
  156. Array2<T>::insert (const Array2<T>& a, int r, int c)
  157. {
  158.   int a_rows = a.rows ();
  159.   int a_cols = a.cols ();
  160.  
  161.   if (r < 0 || r + a_rows > rows () || c < 0 || c + a_cols > cols ())
  162.     {
  163.       (*current_liboctave_error_handler) ("range error for insert");
  164.       return *this;
  165.     }
  166.  
  167.   for (int j = 0; j < a_cols; j++)
  168.     for (int i = 0; i < a_rows; i++)
  169.       elem (r+i, c+j) = a.elem (i, j);
  170.  
  171.   return *this;
  172. }
  173.  
  174. /*
  175. ;;; Local Variables: ***
  176. ;;; mode: C++ ***
  177. ;;; End: ***
  178. */
  179.